package co.crossoft.exposeServiceImpl;

import co.crossoft.base.common.JsonResult;
import co.crossoft.daoImpl.WorkCenterDaoImpl;
import co.crossoft.domain.CommonDialog;
import co.crossoft.domain.ResourceWorkCenter;
import co.crossoft.domain.WorkCenter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import co.crossoft.service.ExposeWorkCenterService;

import java.util.ArrayList;

/**
 * Created by Snow on 8/22/2017.
 */
@Component
public class ExposeWorkCenterServiceImpl implements ExposeWorkCenterService {

    @Autowired
    WorkCenterDaoImpl workCenterDaoImpl;

    /**
     * 根据工作中心模糊查询工作中心数据
     *
     * @param plant      工厂
     * @param workCenter 工作中心
     * @return
     */
    @Override
    public JsonResult selectByWorkCenter(String plant, String workCenter) {
        return workCenterDaoImpl.getWorkCenterList(plant, workCenter);
    }

    /**
     * @param handle 工作中心主键
     * @return
     */
    @Override
    public JsonResult deleteByPrimaryKey(String handle) {
        return workCenterDaoImpl.deleteWorkCenter(handle);
    }

    /**
     * 更新或新增工作中心数据
     *
     * @param plant                 工厂
     * @param workCenter            工作中心
     * @param description           描述
     * @param statusFk              状态FK
     * @param canBeReleasedTo       车间作业控制下达至此工作中心 Y:N
     * @param wcCategory            工作中心类别
     * @param wcType                工作中心类型
     * @param assignmentEnforcement 强制分配
     * @return
     */
    @Override
    public JsonResult addOrUpdateWorkCenter(String plant, String workCenter, String description, String statusFk, String canBeReleasedTo, String wcCategory, String wcType, String assignmentEnforcement) {
        WorkCenter workCenterBean = new WorkCenter();
        workCenterBean.setHandle("WORKCENTER:" + plant + "," + workCenter);
        workCenterBean.setPlant(plant);
        workCenterBean.setWorkCenter(workCenter);
        workCenterBean.setAssignmentEnforcement(assignmentEnforcement);
        workCenterBean.setDescription(description);
        workCenterBean.setStatusFk(statusFk);
        workCenterBean.setWcType(wcType);
        workCenterBean.setWcCategory(wcCategory);
        workCenterBean.setCanBeReleasedTo(canBeReleasedTo);
        return workCenterDaoImpl.addOrUpdateWorkCenter(workCenterBean);
}

    /**
     * @param plant 工厂
     * @param type  类型 WORK_CENTER：工作中心 / RESOURCE：资源
     * @param name  资源或工作中心 模糊检索名称
     * @return
     */
    @Override
    public JsonResult workCenterOrResourceSelect(String plant, String type, String name) {
        CommonDialog commonDialog = new CommonDialog();
        commonDialog.setPlant(plant);
        commonDialog.setName(name);
        commonDialog.setType(type);
        return workCenterDaoImpl.workCenterOrResourceSelect(commonDialog);
    }

    /**
     * @param handleData
     * @param workCenterHandle
     * @return
     */
    @Override
    public JsonResult deleteWorkRefResource(ArrayList<String> handleData, String workCenterHandle) {
        return workCenterDaoImpl.deleteWorkRefResource(handleData, workCenterHandle);
    }

    /**
     * @param sequence
     * @param resourceOrWorkcenterFk
     * @param workCenterFk
     * @param type
     * @return
     */
    @Override
    public JsonResult addResourceRefWork(String sequence, String resourceOrWorkcenterFk, String workCenterFk, String type) {
        ResourceWorkCenter resourceWorkCenter = new ResourceWorkCenter();
        resourceWorkCenter.setHandle(resourceOrWorkcenterFk + ":" + workCenterFk + ":" + sequence);
        resourceWorkCenter.setSequence(new Double(sequence));
        resourceWorkCenter.setType(type);
        resourceWorkCenter.setResourceOrWorkcenterFk(resourceOrWorkcenterFk);
        resourceWorkCenter.setWorkCenterFk(workCenterFk);
        return workCenterDaoImpl.addWorkCenterRefResource(resourceWorkCenter);
    }

    /**
     * @param handle
     * @return
     */
    @Override
    public JsonResult selectOneworkCenterByHandle(String handle) {
        return workCenterDaoImpl.selectOneworkCenterByHandle(handle);
    }
}
